<template>
	<div class="back_contain_n mine-account">
		<div class="mainTable">
			<!-- 头部搜索 -->
			<div class="Search_Top_Part">
				<div class="search_list" >
					<div style="margin-left: 42px;">
						<div class="input_flex">
							<el-select clearable  v-model="queryForm.name" placeholder="申报人" style="width: 200px">
								<el-option v-for="(item,index) in userList" :key="index" :label="item.userName"
										   :value="item.userName"  />
							</el-select>
						</div>
						<div class="input_flex">
							<el-select clearable v-model="queryForm.itemId" placeholder="品类">
								<el-option v-for="(item,index) in itemList" :key="index" :label="item"
										:value="item"/>
							</el-select>
						</div>
						<div class="input_flex daterange">
							<el-date-picker v-model="time"
								type="daterange"
								range-separator="-"
								start-placeholder="开始日期"
								end-placeholder="结束日期">
							</el-date-picker>
						</div>
					</div>
				</div>
				<div class="search_bt">
					<span class="zll-search" @click="getList()">搜索</span>
					<span class="zll-search-reset" @click="searchReset()">重置</span>
				</div>
			</div>
			<!-- table -->
			<sys-table
				ref="systable"
				:isMultipleSelection="false"
				:tableData="tableData"
				:tableLoading="tableLoading"
				:tableHeader="tableHeader"
				:scopeWidth="120"
				:backCount="backCount"
				@getBackData="getList"
			>
				<template slot-scope="scope" slot="operate">
					<el-button @click="edit(scope.row,'edit')" v-if="scope.row.deduceStatus == '0'" type="text" size="small">核定</el-button>
					<el-button @click="edit(scope.row,'see')" type="text" size="small">查看</el-button>
				</template>
			</sys-table>
			
			<!---- 弹框-------->
			<div class="zll-dialog">
				<popout :title="'不良品扣减 · ' + (type == 'edit'?'核定':(type =='see'?'查看':'申报'))" :visible.sync="addDialog" v-if="addDialog" class="bg_maxSize">
					<add ref="add" slot="content" :type="type" :editData="editData" :singlePeople="singlePeople" ></add>
					<template slot="bottom">
						<p class="zll-botton Info" @click="addDialog = false">关 闭</p>
						<p class="zll-botton reject" v-if="type !== 'see'" @click="()=>{this.$refs.add.submitForm('addForm')}">立即扣减</p>
					</template>
				</popout>
			</div> 
        </div>
	</div>
</template>

<script>
 	import Add from './add'
	import {formatDate} from "@/plugins/formatDate";

    export default {
        data(){
            return {
                tableLoading:true, //table刷新
				type: '',
				editData: {},
				itemList:['单支连排','多支','伸缩','U型'],

				queryForm: {
					deduceStatus: '',
					name: '',
					itemId: '',
				},
				backCount: 0,
				tableData: [],
				tableHeader: [],
				addDialog: false,
				time: [],
				status:'',
				singlePeople:''
            }
        },
        methods: {
			init(val){
				this.status = val;
				this.searchReset();
			},

			getList(startIndex, pageRow) {
				let self = this;
				self.tableLoading = true;
				if (this.time && this.time.length == 2) {
					//date类型
					self.queryForm.startTime = formatDate(new Date(self.time[0]), 'yyyy-MM-dd')
					self.queryForm.endTime = formatDate(new Date(self.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
				} else {
					self.queryForm.startTime = ''
					self.queryForm.endTime = ''
				}
				let pageInfo = self.$refs.systable.getPageInfo(startIndex);
				if (pageInfo.pageNum == 0||startIndex == 0) {
					self.$refs.systable.resetPage();
				}
				self.queryForm.startIndex = pageInfo.pageNum;
				self.queryForm.totalPage = pageInfo.pageSize;
				let param = Object.assign({}, self.queryForm);
				self.$http({
					url: "/jinl/reduce/getOnDutyReduceList",
					method: "post",
					params:param
				}).then(resp => {
					if (resp.success) {
						self.tableLoading = false;
						self.tableData = resp.result;
						self.tableHeader = [
							{"columnValue": "applyTime", "columnName": "申报日期"},
							{"columnValue": "applyUser", "columnName": "申报人"},
							{"columnValue": "dutyDayTask", "columnName": "生产日期"},
							{"columnValue": "itemRefName", "columnName": "品类"},
							{"columnValue": "planName", "columnName": "任务名称"},
							{"columnValue": "gradeValue", "columnName": "规格"},
							{"columnValue": "userNames", "columnName": "姓名"},
							{"columnValue": "deducePrice", "columnName": "扣减金额"},
							{"columnValue": "deleteCount", "columnName": "扣减件数"},
							{"columnValue": "deduceStatusCn", "columnName": "状态",refColor:true},
							{"columnValue": "checkUser", "columnName": "核定人"},
							{"columnValue": "checkTime", "columnName": "核定日期"},
						];
						if (resp.result.length > 0) {
							self.backCount = resp.result[0].count;
						} else {
							self.backCount = 0;
						}
					}
				});

			},

			getOnDutyReduceDetailList(startIndex, pageRow) {
				let self = this;
				self.tableLoading = true;
				if (this.time && this.time.length == 2) {
					//date类型
					self.queryForm.startTime = formatDate(new Date(self.time[0]), 'yyyy-MM-dd')
					self.queryForm.endTime = formatDate(new Date(self.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
				} else {
					self.queryForm.startTime = ''
					self.queryForm.endTime = ''
				}
				let pageInfo = self.$refs.systable.getPageInfo(startIndex);
				if (pageInfo.pageNum == 0||startIndex == 0) {
					self.$refs.systable.resetPage();
				}
				self.queryForm.startIndex = pageInfo.pageNum;
				self.queryForm.totalPage = pageInfo.pageSize;
				let param = Object.assign({}, self.queryForm);
				self.$http({
					url: "/jinl/reduce/getOnDutyReduceDetailList",
					method: "post",
					params:param
				}).then(resp => {
					if (resp.success) {
						self.tableLoading = false;
						self.tableData = resp.result;
						self.tableHeader = [
							{"columnValue": "applyTime", "columnName": "申报日期"},
							{"columnValue": "applyUser", "columnName": "申报人"},
							{"columnValue": "dutyDayTask", "columnName": "生产日期"},
							{"columnValue": "itemRefName", "columnName": "品类"},
							{"columnValue": "planName", "columnName": "任务名称"},
							{"columnValue": "gradeValue", "columnName": "规格"},
							{"columnValue": "userName", "columnName": "姓名"},
							{"columnValue": "singleReducePrice", "columnName": "扣减金额"},
							{"columnValue": "deduceStatusCn", "columnName": "状态",refColor:true},
							{"columnValue": "checkUser", "columnName": "核定人"},
							{"columnValue": "checkTime", "columnName": "核定日期"},
						];
						if (resp.result.length > 0) {
							self.backCount = resp.result[0].count;
						} else {
							self.backCount = 0;
						}
					}
				});

			},

            edit(val,type) {
				let self = this;
				self.addDialog = true;
				self.type = type;
				self.singlePeople = self.status !='2'?'':'1';
				self.$nextTick(() => {
					self.editData = Object.assign({}, val);
				});
            },
            searchReset() { //重置搜索
                this.queryForm = {
					deduceStatus: this.status,
					name: '',
					itemId: '',
				};
                if(this.status == '0'||this.status == '1'){
					this.getList();
				}else{
					this.queryForm.deduceStatus = '1';
					this.getOnDutyReduceDetailList();
				}

            },

			getSelectUsers() {
				let self = this;
				self.userList = [];
				self.$http({
					url: "/jinl/login/getLoginAndRoleSelectList",
					method: "post",
				}).then(resp => {
					if (resp.success) {
						self.userList = resp.result;
					}
				});
			},

		},
        mounted(){
			let self = this;
            self.getSelectUsers();
			self.$eventBus.$on("updateOndutyCheckList",function () {
				self.addDialog = false;
				self.searchReset()
			})
        },
        components: {
            Add
        },
    }
</script>

<style lang="scss" scoped>
	@import "@/style/SearchTop.scss";

	.mine-account {
		.search_list {
			display: flex;
			justify-content: space-between;
		}
	}
</style>
